package com.luginf.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import com.luginf.bo.Summary;
import com.luginf.utils.BigDecimalUtils;
import com.luginf.utils.DateUtils;

import com.luginf.activities.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class SummaryELAdapter extends BaseExpandableListAdapter {

	private List<Summary> _summary = null;
	private Context _context = null;

	public SummaryELAdapter(Context context) {
		_context = context;
		_summary = new ArrayList<Summary>();
	}
	
	public SummaryELAdapter(Context context, List<Summary> summary) {
		_context = context;
		_summary = summary;
	}

	public Summary getChild(int groupPosition, int childPosition) {
		if (_summary == null) {
			return null;
		}
		return _summary.get(groupPosition);
	}

	public long getChildId(int groupPosition, int childPosition) {
		return groupPosition;
	}

	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.view_summary_child, null);
		}
		// Get summary line
		Summary summary = getChild(groupPosition, childPosition);
		String price = BigDecimalUtils.formatToUser(summary.getPrice(), Locale.getDefault());
		
		// Set info group
		((TextView) convertView.findViewById(R.id.view_summary_child_label)).setText(summary.getLabel());
		((TextView) convertView.findViewById(R.id.view_summary_child_price)).setText(price);
		return convertView;
	}

	public int getChildrenCount(int groupPosition) {
		return 1;
	}

	public Summary getGroup(int groupPosition) {
		if (_summary == null) {
			return null;
		}
		return _summary.get(groupPosition);
	}

	public int getGroupCount() {
		return _summary.size();
	}

	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater infalInflater = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = infalInflater.inflate(R.layout.view_summary_group, null);
		}
		// Get summary line
		Summary summary = getGroup(groupPosition);
		String date = DateUtils.formatDateToLocale(summary.getDate(), Locale.getDefault());

		// Set info group
		((TextView) convertView.findViewById(R.id.view_summary_group_date)).setText(date);
		((TextView) convertView.findViewById(R.id.view_summary_group_buyer)).setText(summary.getBuyer().getName());
		return convertView;
	}

	public boolean hasStableIds() {
		return true;
	}

	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}
}